package com.medaxis.hpams.sso.controller;



import com.medaxis.hpams.dto.global.BaseResponse;
import com.medaxis.hpams.dto.global.ErrorException;
import com.medaxis.hpams.dto.global.SuccessResponse;
import com.medaxis.hpams.sso.common.RedisCache;
import com.medaxis.hpams.sso.constant.Constants;
import com.medaxis.hpams.sso.domain.dto.LoginBody;
import com.medaxis.hpams.sso.service.LoginService;
import com.medaxis.hpams.sso.utils.VerifyCodeUtils;
import com.medaxis.hpams.sso.utils.base64.Base64;
import com.medaxis.hpams.sso.utils.uuid.IdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 验证码操作处理
 */
@RestController
@RequestMapping(Constants.VERSION_NUMBER+"/sso/user")
@Api(tags = Constants.VERSION_NUMBER+"用户登录")
public class LoginController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private LoginService loginService;

//    @Autowired
//    private SysPermissionService permissionService;
//
//    @Autowired
//    private TokenService tokenService;


    @ApiOperation("获取验证码图片")
    @GetMapping("/captchaImage")
    public BaseResponse captchaImage() {
        // 生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        // 唯一标识
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        // 生成图片
        int w = 111, h = 36;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            VerifyCodeUtils.outputImage(w, h, stream, verifyCode);
            HashMap<String, String> object = new HashMap<>();
            object.put("uuid",uuid);
            object.put("img", Base64.encode(stream.toByteArray()));
            return SuccessResponse.success(object);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorException(e.getMessage());
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    /**
     * 登录方法
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public BaseResponse login(@RequestBody LoginBody loginBody) {

        Map<String, String> map=null;
        // 生成令牌
        String  token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());

        if (token.equals("0")){
            throw  new ErrorException(Constants.GET_CODE_ERROR);
        }

        else if (token.equals("1")){
            throw  new ErrorException(Constants.CODE_WRONG);

        }

        else if (token.equals("2")){
            throw  new ErrorException(Constants.ACCOUNT_OR_PASSWORD_ERROR);
        }

        else {
            map  = new HashMap<>();
            map.put(Constants.TOKEN, token);
        }

        return SuccessResponse.success(map);
    }
}
